﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace blackhouse.WebNemo.Web {

    [Obsolete]
    public class NemoUrlUtils {

        private NemoUrlUtils() { }

        public static NemoUrlUtils GetUrlUtils() {
            return new NemoUrlUtils();
        }

        public string GetDisplayRecord(int groupId, int recordId) {
            return GetDisplayRecord(groupId, recordId, DisplayModPopup);
        }

        public virtual string GetDisplayRecord(int groupId, int recordId, string display) {
            return string.Format("{0}DisplayRecord.aspx?groupId={2}&recordId={1}&{3}", GetStartUrl(), recordId, groupId, this.GetDisplayParam(display));
        }

        public string GetEditRecord(int groupId, int recordId) {
            return GetEditRecord(groupId, recordId, DisplayModNormal, false);
        }

        public virtual string GetEditRecord(int groupId, int recordId, bool autoClose) {
            return GetEditRecord(groupId, recordId, DisplayModNormal, autoClose);
        }

        public virtual string GetEditRecord(int groupId, int recordId, string display) {
            return GetEditRecord(groupId, recordId, display, false);
        }

        public virtual string GetEditRecord(int groupId, int recordId, string display, bool autoClose) {
            string url = string.Format("{0}EditRecord.aspx?groupId={2}&recordId={1}&{3}", GetStartUrl(), recordId, groupId, this.GetDisplayParam(display));
            if (autoClose)
                url += "&autoClose=1";
            return url;
        }

        public string GetEditReference(int groupId, int recordId, int refGroupId, string refColumn, int[] selected) {
            return GetEditReference(groupId, recordId, refGroupId, refColumn, selected, DisplayModPopup);
        }

        public virtual string GetEditReference(int groupId, int recordId, int refGroupId, string refColumn, int[] selected, string display) {
            string url = string.Format("{0}EditReference.aspx?groupId={2}&recordId={1}&refGroupId={3}&refColumn={4}", GetStartUrl(), recordId, groupId, refGroupId, refColumn);
            if (selected.Length > 0)
                url = url + "&selected=" + string.Join(",", selected);
            if (display != DisplayModNormal)
                url = url + "&" + this.GetDisplayParam(display);
            return url;
        }

        public string GetDisplayRecords(int groupId) {
            return GetDisplayRecords(groupId, GetCurrentDisplay());
        }

        public string GetDisplayRecords(int groupId, string display) {
            return string.Format("{0}GroupListing.aspx?groupId={1}&{2}", GetStartUrl(), groupId, this.GetDisplayParam(display));
            throw new NotImplementedException();
        }

        private static string startUrl;

        public static string GetStartUrl() {
            if (string.IsNullOrEmpty(startUrl)) {
                string loc = ConfigurationManager.AppSettings["NemoLocation"];
                if (string.IsNullOrEmpty(loc))
                    startUrl = "/";
                else {
                    startUrl = loc;
                    if (!startUrl.StartsWith("/")) startUrl = "/" + startUrl;
                    if (!startUrl.EndsWith("/")) startUrl = startUrl + "/";
                }
            }
            return startUrl;
        }

        private string GetCurrentDisplay() {
            return HttpContext.Current.Request["mod"];
        }

        private string GetDisplayParam(string display) {
            string ds = "";

            if (display != DisplayModNormal) {
                ds = "mod=" + display;
                if (display == GetCurrentDisplay()) {
                    int dsi = 0;
                    if (!Int32.TryParse(HttpContext.Current.Request["modi"], out dsi))
                        dsi = 1;
                    dsi++;
                    ds += "&modi=" + dsi;
                }
            }
            else {
                ds = GetCurrentDisplay();
                if (!string.IsNullOrEmpty(ds)) ds = "mod=" + ds;
                else ds = "";
            }

            string reff = HttpContext.Current.Request["refferUrl"];
            if (!string.IsNullOrEmpty(reff))
                ds = ds + "&refferUrl=" + HttpUtility.UrlEncode(reff);

            return ds;
        }


        public string GetEditGroup(int p, Business.Groups.GroupType groupType) {
            throw new NotImplementedException();
        }

        public static string DisplayModNormal { get { return ""; } }
        public static string DisplayModPopup { get { return "popup"; } }
        public static string DisplayModFullWidth { get { return "fullw"; } }


    }
}